﻿using FRMS.BUS;
using FRMS.Model;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
using System;

namespace FRMS.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    [Authorize]
    public class HomeController : ControllerBase
    {
        private JwtSettings _jwtSettings=new JwtSettings();
        IHttpContextAccessor _access;
        public HomeController(IOptions<JwtSettings> jwtSettingsAccesser, IHttpContextAccessor access
         )
        {
            JwtSettings jwtSettings = new JwtSettings();
            jwtSettings.Audience = AppSettingsHelper.Configuration["JwtSettings:Audience"];
            jwtSettings.Issuer = AppSettingsHelper.Configuration["JwtSettings:Issuer"];
            jwtSettings.SecretKey = AppSettingsHelper.Configuration["JwtSettings:SecretKey"];
            _jwtSettings = jwtSettings;
            //_jwtSettings = jwtSettingsAccesser.Value;
            _access = access;
        }

        /// <summary>
        /// 查询菜单
        /// </summary>
        [HttpPost]
        [Route("QueryMenu")]
        public ResponseMessage QueryMenu()
        {
            ResponseMessage message = new ResponseMessage();
            HomeBUS BUS = new HomeBUS();
            try
            {
                message = BUS.QueryMenu();
            }
            catch (Exception e)
            {
                DataClearBUS dataClearBUS = new DataClearBUS();
                ErrorLog errorLog = new ErrorLog();
                errorLog.ErrorMsg = e.ToString();
                errorLog.Operation = "载入菜单";
                dataClearBUS.AddErrorLog(errorLog);
                message.errcode = 1;
                message.errmsg = "载入菜单异常";
                message.data = null;
                message.total = 0;
            }
            return message;
        }

        /// <summary>
        /// 获取当前用户
        /// </summary>
        [HttpPost]
        [Route("GetUserInfo")]
        public ResponseMessage GetUserInfo()
        {
            ResponseMessage message = new ResponseMessage();
            HomeBUS BUS = new HomeBUS();
            try
            {
                if (_access.HttpContext.Session.GetString("UserId") != null)
                {
                    string result = _access.HttpContext.Session.GetString("UserId").ToString();
                    int id = int.Parse(result);
                    message = BUS.LoadUser(id);
                }
                else
                {
                    message.errcode = 1;
                    message.errmsg = "获取当前用户异常";
                    message.data = null;
                    message.total = 0;
                }
            }
            catch (Exception e)
            {
                DataClearBUS dataClearBUS = new DataClearBUS();
                ErrorLog errorLog = new ErrorLog();
                errorLog.ErrorMsg = e.ToString();
                errorLog.Operation = "获取当前用户信息";
                dataClearBUS.AddErrorLog(errorLog);
                message.errcode = 1;
                message.errmsg = "获取当前用户异常";
                message.data = null;
                message.total = 0;
            }
            return message;
        }

        /// <summary>
        /// 获取当前项目菜单
        /// </summary>
        [HttpPost]
        [Route("GetMenuByProject")]
        public ResponseMessage GetMenuByProject()
        {
            ResponseMessage message = new ResponseMessage();
            HomeBUS BUS = new HomeBUS();
            try
            {
                string result = _access.HttpContext.Session.GetString("ProjectID").ToString();
                int projectid = int.Parse(result);
                string name = _access.HttpContext.Session.GetString("LoginName").ToString();
                message = BUS.GetMenuByProject(projectid, name);
            }
            catch (Exception e)
            {
                DataClearBUS dataClearBUS = new DataClearBUS();
                ErrorLog errorLog = new ErrorLog();
                errorLog.ErrorMsg = e.ToString();
                errorLog.Operation = "获取当前项目菜单信息";
                dataClearBUS.AddErrorLog(errorLog);
                message.errcode = 1;
                message.errmsg = "获取当前项目菜单异常";
                message.data = null;
                message.total = 0;
            }
            return message;
        }

        /// <summary>
        /// 获取指定项目菜单
        /// </summary>
        [HttpPost]
        [Route("GetMenuByProjectID")]
        public ResponseMessage GetMenuByProjectID(int proid)
        {
            ResponseMessage message = new ResponseMessage();
            HomeBUS BUS = new HomeBUS();
            try
            {
                message = BUS.GetMenuByProject(proid);
            }
            catch (Exception e)
            {
                DataClearBUS dataClearBUS = new DataClearBUS();
                ErrorLog errorLog = new ErrorLog();
                errorLog.ErrorMsg = e.ToString();
                errorLog.Operation = "获取指定项目菜单信息";
                dataClearBUS.AddErrorLog(errorLog);
                message.errcode = 1;
                message.errmsg = "获取菜单异常";
                message.data = null;
                message.total = 0;
            }
            return message;
        }

        /// <summary>
        /// 获取用户当前功能按钮
        /// </summary>
        [HttpPost]
        [Route("GetFoptionByUser")]
        public ResponseMessage GetFoptionByUser(int userid, string url)
        {
            ResponseMessage message = new ResponseMessage();
            HomeBUS BUS = new HomeBUS();
            try
            {
                string username = _access.HttpContext.Session.GetString("LoginName").ToString();
                message = BUS.GetFoption(userid, url, username);
            }
            catch (Exception e)
            {
                DataClearBUS dataClearBUS = new DataClearBUS();
                ErrorLog errorLog = new ErrorLog();
                errorLog.ErrorMsg = e.ToString();
                errorLog.Operation = "获取当前用户菜单信息";
                dataClearBUS.AddErrorLog(errorLog);
                message.errcode = 1;
                message.errmsg = "获取当前菜单异常";
                message.data = null;
                message.total = 0;
            }
            return message;
        }

        /// <summary>
        /// 切换当前项目
        /// </summary>
        [HttpPost]
        [Route("SwitchProject")]
        public ResponseMessage SwitchProject(int projectid)
        {
            ResponseMessage message = new ResponseMessage();
            try
            {
                ProjectBUS bUS = new ProjectBUS();
                _access.HttpContext.Session.SetString("ProjectID", projectid.ToString());
                message = bUS.SwitchProject(projectid);
            }
            catch (Exception e)
            {
                DataClearBUS dataClearBUS = new DataClearBUS();
                ErrorLog errorLog = new ErrorLog();
                errorLog.ErrorMsg = e.ToString();
                errorLog.Operation = "切换项目";
                dataClearBUS.AddErrorLog(errorLog);
                message.errcode = 1;
                message.errmsg = "切换项目异常";
                message.data = null;
                message.total = 0;
            }
            return message;
        }

        /// <summary>
        /// 获取当前项目
        /// </summary>
        [HttpPost]
        [Route("GetProject")]
        public ResponseMessage GetProject()
        {
            ResponseMessage message = new ResponseMessage();
            try
            {
                int projectid = Convert.ToInt32(_access.HttpContext.Session.GetString("ProjectID"));
                message.errcode = 0;
                message.data = projectid;
            }
            catch (Exception e)
            {
                DataClearBUS dataClearBUS = new DataClearBUS();
                ErrorLog errorLog = new ErrorLog();
                errorLog.ErrorMsg = e.ToString();
                errorLog.Operation = "切换项目";
                dataClearBUS.AddErrorLog(errorLog);
                message.errcode = 1;
                message.errmsg = "获取项目异常";
                message.data = null;
                message.total = 0;
            }
            return message;
        }

        /// <summary>
        /// 获取所有功能按钮
        /// </summary>
        [HttpPost]
        [Route("GetAllFoption")]
        public ResponseMessage GetAllFoption(int userid)
        {
            ResponseMessage message = new ResponseMessage();
            HomeBUS BUS = new HomeBUS();
            try
            {
                message = BUS.GetAllFoption(userid);
            }
            catch (Exception e)
            {
                DataClearBUS dataClearBUS = new DataClearBUS();
                ErrorLog errorLog = new ErrorLog();
                errorLog.ErrorMsg = e.ToString();
                errorLog.Operation = "获取用户功能按钮";
                dataClearBUS.AddErrorLog(errorLog);
                message.errcode = 1;
                message.errmsg = "获取用户功能按钮异常";
                message.data = null;
                message.total = 0;
            }
            return message;
        }

        /// <summary>
        /// 获取读卡端口
        /// </summary>
        [HttpPost]
        [Route("GetCardPort")]
        public ResponseMessage GetCardPort()
        {
            ResponseMessage message = new ResponseMessage();
            try
            {
                //string port = CommVars.Configuration.GetSection("ICCardPort").Value;
                string port = AppSettingsHelper.Configuration["ICCardPort"];
                message.data = port;
                message.errcode = 0;
            }
            catch (Exception e)
            {
                message.errcode = 1;
            }
            return message;
        }

    }
}
